package wang.ATM;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    private ArrayList<Account> accounts =new ArrayList<>();
    private Scanner sc = new Scanner(System.in);
    private Account loginaccount;
    public    void AtmStart(){
        while (true) {
            System.out.println("===欢迎进入ATM系统===");
            System.out.println("1.用户登录");
            System.out.println("2.开户");
            System.out.println("3.退出");
            int command = sc.nextInt();
            switch (command){
                case 1:
                    login();
                    break;
                case 2:
                    Adduser();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("您输入的命令有误，请重新输入");
            }
        }
    }
    //登录方法
    private void login(){
        System.out.println("===系统登录===");
        if (accounts.size() == 0){
            System.out.println("当前还没有创建账户 请创建完账户后再操作");
            return;
        }
        while (true) {
            System.out.println("请输入您的卡号");
            String cardId = sc.next();
            Account acc = getAccountbyIdcard(cardId);
            if (acc == null){
                System.out.println("您输入的卡号有误 请重新输入");
            }else {
                while (true) {
                    System.out.println("请您输入您的密码");
                    String password = sc.next();
                    if (acc.getPassword().equals(password)){
                        loginaccount = acc;
                        System.out.println("恭喜您" + acc.getName() + "登陆成功，您的卡号是" + acc.getIdCard());
                        showlogincommand();
                        return;
                    }else {
                        System.out.println("您输入的密码有误，请重新输入");
                    }
                }
            }
            }
        }
    //登录后界面
    private void showlogincommand(){
        while (true) {
            System.out.println("===" + loginaccount.getName() + "恭喜您登录成功，请选择您想要办理的业务" + "===");
            System.out.println("1.查询账户");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.退出");
            System.out.println("7.注销账户");
            System.out.println("请选择：");
            int command = sc.nextInt();
            switch (command){
                case 1:
                    showloginaccount();
                    break;
                case 2:
                    depositMoney();
                    break;
                case 3:
                    dropMoney();
                    break;
                case 4:
                    transferMoney();
                    break;
                case 5:
                    UpdatePassword();
                    return;
                case 6:
                    System.out.println("恭喜您" + loginaccount.getName() + "退出系统成功");
                    return; //跳出并结束当前方法
                case 7:
                    if (deleteaccount()){
                        return;
                    }
                    break;
            }
        }
    }
//修改密码
    private void UpdatePassword() {
        System.out.println("==修改密码操作==");
        while (true) {
            System.out.println("请输入当前密码");
            String password = sc.next();
            if (loginaccount.getPassword().equals(password)){
                while (true) {
                    System.out.println("请您输入新密码");
                    String newPassword = sc.next();
                    System.out.println("请您再次输入密码确认");
                    String OkPassword = sc.next();
                    if (newPassword.equals(OkPassword)){
                        loginaccount.setPassword(newPassword);
                        System.out.println("恭喜您 密码修改成功");
                        return;
                    }else {
                        System.out.println("对不起，您两次输入密码不一致");
                    }
                }
            }else {
                System.out.println("当前账户密码错误");
            }
        }
    }

    //销户操作
    private boolean deleteaccount() {
        System.out.println("==销户操作==");
        System.out.println("请问您真的要销户吗？ y/n");
        String command = sc.next();
        switch (command){
            case "y":
             if (loginaccount.getMoney() == 0){
                 accounts.remove(loginaccount);
                 System.out.println("恭喜您销户成功");
                 return true;
             }else {
                 System.out.println("您当前账户余额不为0，无法销户");
                 return false;
             }
            default:
                return false;
        }
    }

    //转账方法
    private void transferMoney() {
        System.out.println("==转账操作==");
        if (accounts.size() < 2){
            System.out.println("当前只有一个账户，无法进行转账操作");
            return;
        }
        if (loginaccount.getMoney() == 0){
            System.out.println("当前账户余额为0，无法转账");
            return;
        }
        while (true) {
            System.out.println("请输入您想要转账的卡号");
            String cardId = sc.next();
            Account acc = getAccountbyIdcard(cardId);
            if (acc == null){
                System.out.println("您输入的卡号不存在，请重新输入");
            }else {
                String perName = "*" +acc.getName().substring(1);
                System.out.println("请您输入[" + perName +"] 的姓氏");
                String name = sc.next();
                if (acc.getName().startsWith(name)){
                    while (true) {
                        System.out.println("请输入您的转账金额");
                        double money = sc.nextDouble();
                        if (money > loginaccount.getMoney()){
                            System.out.println("您当前账户余额不足无法进行转账操作");
                        }else {
                            double balance = loginaccount.getMoney()-money;
                            loginaccount.setMoney(balance);
                            double balance2 = acc.getMoney() + money;
                            acc.setMoney(balance2);
                            return;
                        }
                    }

                }else {
                    System.out.println("您输入的姓氏有问题");
                }
            }
        }
    }

    //取款操作
    private void dropMoney() {
        System.out.println("==取款操作==");
        if (loginaccount.getMoney() < 100){
            System.out.println("当前账户余额不足100无法取现");
            return;
        }
        while (true) {
            System.out.println("请输入您的取款金额");
            double money = sc.nextDouble();
            if (loginaccount.getMoney() >= money){
                if (loginaccount.getLimit() >= money){
                    loginaccount.setMoney(loginaccount.getMoney() - money);
                    System.out.println("恭喜您取款成功，当前账户余额为" + loginaccount.getMoney());
                    return;
                }
                else {
                    System.out.println("您当前取款金额超过单次最大取款金额限制，每次最大取款额度为" + loginaccount.getLimit());
                }
            }else {
                System.out.println("您当前账户余额不足，当前账户余额为" + loginaccount.getMoney());
                System.out.println("您是否还要继续？继续请按1 退出请按2");
                int command = sc.nextInt();
                switch (command){
                    case 1:
                        break;
                    case 2:
                        return;
                }
            }
        }
    }

    //存钱方法
    private void depositMoney(){
        System.out.println("==存钱操作==");
        System.out.println("请输入您的存款金额");
        double money = sc.nextDouble();
        //更新当前账户余额
        loginaccount.setMoney(loginaccount.getMoney() + money);
        System.out.println("恭喜您存款成功" + money + "元，您当前账户余额为：" + loginaccount.getMoney());
        return;

    }
    //查询账户方法
    private void showloginaccount(){
        System.out.println("当前您的账户信息如下");
        System.out.println("卡号：" + loginaccount.getIdCard());
        System.out.println("姓名：" + loginaccount.getName());
        System.out.println("余额：" + loginaccount.getMoney());
        System.out.println("每次取现额度：" + loginaccount.getLimit());
    }
    //添加账户
    private  void Adduser(){
        Account user = new Account();
        System.out.println("===欢迎进入开户系统===");
        Scanner sc = new Scanner(System.in);
        System.out.println("请您输入您的姓名");
        user.setName(sc.next());
        while (true) {
            System.out.println("请您输入您的性别");
            String sex = sc.next();
            if (sex.equals("男")|| sex.equals("女")){
                user.setSexy(sex);
                break;
            }else {
                System.out.println("您输入的性别有误，请重新输入");
            }
        }
        while (true) {
            System.out.println("请输入您的密码");
            String password = sc.next();
            System.out.println("请确认您的密码");
            String Okpassword = sc.next();
            if (password.equals(Okpassword)){
                user.setPassword(password);
                break;
            }else {
                System.out.println("您两次输入的密码不一致，请重新输入");
            }
        }
        System.out.println("请输入您最大的取款金额");
        user.setLimit(sc.nextInt());
        String newcardid = Id();
        user.setIdCard(newcardid);
        accounts.add(user);
        System.out.println("恭喜您" + user.getName() + "账户创建成功" + "您的卡号是" + user.getIdCard());

    }
    //创建随机卡号
    private  String Id(){
        while (true) {
            Random data = new Random();
            String id = "";
            for (int i = 0; i < 8; i++) {
                int r = data.nextInt(10);
                id += r;
            }
            Account acc = getAccountbyIdcard(id);
            if (acc == null){
                return id;
            }
        }
    }
    //确保随机卡号不重复
        private Account getAccountbyIdcard(String idcard){
            for (int i = 0; i < accounts.size(); i++) {
                Account acc = accounts.get(i);
                if (acc.getIdCard().equals(idcard)){
                    return acc;
                }
            }
            return null;
        }
    }


